// stores/counter.js
import {
	defineStore
} from 'pinia'
import {
	ref,
	computed
} from 'vue'

// 1. 选项式 Store

// 这里省略上一小节代码

// 2. 组合式 Store
export const useUsercodeStore = defineStore('usercode', () => {
	// 定义 state
	const usercode = ref({
		token: '',
		userId: '',
		isAddress: 0,
		user: '',
		nickname: '',
		himg: '',
		animation: '',
		firstTX: '',
		firstWX: '',
		prompt: 0,
		mobile:''
	})

	// 定义 getters
	// const setAddress = computed((add) => usercode.value.isAddress = add)

	function setAddress(add) {
		usercode.value.isAddress = add
	}

	function setAnimation(add) {
		usercode.value.animation = add
	}

	function setHimg(add) {
		usercode.value.himg = add
	}

	function setNickname(add) {
		usercode.value.nickname = add
	}

	function setFirstTX(add) {
		usercode.value.firstTX = add
	}

	function setFirstWX(add) {
		usercode.value.firstWX = add
	}

	function resetToken() {
		usercode.value.token = ''
		usercode.value.userId = ''
		usercode.value.user = ''
	}

	function setPrompt(add) {
		usercode.value.prompt = add
	}

	// 定义 actions
	function setUsercode(res) {
		uni.request({
			url: 'https://card.wm2177.com/api/empower/empower', // 后端接口URL地址
			method: 'POST', // 请求方法，根据后端API可以选择'GET'、'POST'等
			data: {
				code: res.code,
				mobile_code: res.mobile,
			},
			success: (res) => {
				// 请求成功的回调函数
				console.log(res.data)
				if (res.data.st == 1) {
					usercode.value.isAddress = res.data.data.is_address
					usercode.value.token = res.data.data.token
					usercode.value.userId = res.data.data.id
					usercode.value.user = res.data.data.user_id
					usercode.value.himg = res.data.data.himg
					usercode.value.animation = res.data.data.is_animation
					usercode.value.nickname = res.data.data.nickname
					usercode.value.firstTX = res.data.data.first_tx
					usercode.value.firstWX = res.data.data.first_wx
					usercode.value.mobile = res.data.data.mobile
					uni.switchTab({
						url: '/pages/home/index',
					})
				} else {
					uni.utils.toast(res.data.msg)
				}
			},
			fail: (err) => {
				// 请求失败的回调函数
				uni.utils.toast(err.errMsg)
				console.error(err)
			},
		})
	}

	function appSetUsercode(res) {
		uni.request({
			url: 'https://card.wm2177.com/api/empower/loginPost', // 后端接口URL地址
			method: 'POST', // 请求方法，根据后端API可以选择'GET'、'POST'等
			data: {
				code: res.code,
				mobile: res.mobile,
			},
			success: (res) => {
				// 请求成功的回调函数
				console.log(res.data)
				if (res.data.st == 1) {
					usercode.value.isAddress = res.data.data.is_address
					usercode.value.token = res.data.data.token
					usercode.value.userId = res.data.data.id
					usercode.value.user = res.data.data.user_id
					usercode.value.himg = res.data.data.himg
					usercode.value.animation = res.data.data.is_animation
					usercode.value.nickname = res.data.data.nickname
					usercode.value.firstTX = res.data.data.first_tx
					usercode.value.firstWX = res.data.data.first_wx
					usercode.value.mobile = res.data.data.mobile
					uni.switchTab({
						url: '/pages/home/index',
					})
				} else {
					uni.utils.toast(res.data.msg)
				}
			},
			fail: (err) => {
				// 请求失败的回调函数
				uni.utils.toast(err.errMsg)
				console.error(err)
			},
		})
	}

	function appPassword(res) {
		uni.request({
			url: 'https://card.wm2177.com/api/empower/passwordLogin', // 后端接口URL地址
			method: 'POST', // 请求方法，根据后端API可以选择'GET'、'POST'等
			data: {
				mobile: res.code,
				password: res.password,
			},
			success: (res) => {
				// 请求成功的回调函数
				console.log(res.data)
				if (res.data.st == 1) {
					usercode.value.isAddress = res.data.data.is_address
					usercode.value.token = res.data.data.token
					usercode.value.userId = res.data.data.id
					usercode.value.user = res.data.data.user_id
					usercode.value.himg = res.data.data.himg
					usercode.value.animation = res.data.data.is_animation
					usercode.value.nickname = res.data.data.nickname
					usercode.value.firstTX = res.data.data.first_tx
					usercode.value.firstWX = res.data.data.first_wx
					usercode.value.mobile = res.data.data.mobile
					uni.switchTab({
						url: '/pages/home/index',
					})
				} else {
					uni.utils.toast(res.data.msg)
				}
			},
			fail: (err) => {
				// 请求失败的回调函数
				uni.utils.toast(err.errMsg)
				console.error(err)
			},
		})
	}

	function delUsercode() {
		usercode.value.token = ''
		usercode.value.userId = ''
	}

	// 千万不要忘记这里要 return
	return {
		useUsercodeStore,
		appSetUsercode,
		appPassword,
		usercode,
		setUsercode,
		delUsercode,
		setAddress,
		setAnimation,
		resetToken,
		setHimg,
		setNickname,
		setFirstTX,
		setFirstWX,
		setPrompt
	}
}, {
	persist: {
		path: ['usercode']
	},
})